<?php
	class GeoMapHelper extends AppHelper
	{
		var $helpers = array("Html",'Javascript','Rating');
		function getInfobxoHtml($listing)
		{
			uses("flay");
			$disableEditorReviews = cr('GeneralConfig.disable_editor_reviews');
			$userReviewCount = '<div style="float:left;margin-left:80px">('.$listing['Listing']['review_count'].')</div>';
			$userRating = $listing['Listing']['userAvg'];
			$editorRating = $listing['Listing']['editorialAvg'];
			$userRatingHtml = $this->Rating->display(array('rating'=>$userRating,'before_text'=>$userReviewCount,'tooltip'=>__('User Rating').' '.number_format($userRating,1),'tooltipClass'=>'user_'.$listing['Listing']['id'].'_'.mt_rand(1,1000)));
			$editorRatingHtml = "";
			if($disableEditorReviews != 1)
			{
				$editorRatingHtml = $this->Rating->display(array('rating'=>$editorRating,'class'=>'editor_rating_star_value', 'parent_class'=>'editor_rating_star_base', 'tooltip'=>__('Editor Rating').' '.number_format($editorRating,1),'tooltipClass'=>'editor_'.$listing['Listing']['id'].'_'.mt_rand(1,1000)));
			}
			$listing['Listing']['editorRatingHtml'] = $userRatingHtml;
			$listing['Listing']['userRatingHtml'] = $editorRatingHtml;
			$listing['Listing']['title'] = Flay::fragment($listing['Listing']['title'],20);
			$listing['Listing']['viewUrl'] = $this->Html->url($listing['Listing']['viewUrl']);
			$imageHtml = $this->Html->image($listing['Listing']['image'],array('thumb'=>cr('Thumbs.geoMap'), 'height'=>cr('Thumbs.geoMap.height'), 'width'=>cr('Thumbs.geoMap.width')));
			//prd(htmlentities($listing['Listing']['imageHtml']));
			$template = '<div id="listing_infobox_:Listing.id" class="listing_map_info">
				:imageHtml
				<div>
				<div>
					<a href=":Listing.viewUrl">:Listing.title</a>
				<div>
				<div class="ratings">
				</div>
				<div>
					:Listing.userRatingHtml
				</div>
				<div>
					:Listing.editorRatingHtml
				</div>
				<div>
			</div>';
			$listingTemplateArray = Utility::dotNotation($listing);
			$listingHtml = String::insert($template,$listingTemplateArray);
			$listingHtml = String::insert($listingHtml,compact('imageHtml'));
			$listingHtml = "<div class=\"map_content\">".$listingHtml."</div>";
			return $listingHtml;
		}
		function getListingsMarkerJson($listings)
		{
			$markers = $this->getListingsMarker($listings);
			$markersJson = $this->Javascript->object($markers);
			return $markersJson;
		}
		function getListingsMarker($listings)
		{
			$markers = array();
			foreach($listings as $listing)
			{
				if(!$listing['Listing']['lat'] || !$listing['Listing']['lng'])
				{
					continue;
				}
				$marker['constructOptions']['title'] = $listing['Listing']['title'];
				$marker['constructOptions']['address'] = $this->getAddress($listing);
				$marker['lat'] = $listing['Listing']['lat'];
				$marker['lng'] = $listing['Listing']['lng'];
				$marker['id'] = $listing['Listing']['id'];
				$icon = $this->getIconUrl($listing);
				if($icon)
				{
					$marker['constructOptions']['icon'] =	$icon; 
				}
				$marker['info'] = $this->getInfobxoHtml($listing);
				$markers[] = $marker;
			}
			return $markers;
		}
		
		private function getAddress($listing)
		{
			$address = "";
			$fields = array('address1_field','address2_field','state','city','postal_code','country');
			foreach($fields as $field)
			{
				$listingFieldname = cr("MapConfig.".$field);
				if($listingFieldname && isset($listing['Listing'][$listingFieldname]))
				{
					$address .= ' '.$listing['Listing'][$listingFieldname];
				}
			}
			return $address;
		}
		
		private function getIconUrl($listing)
		{
			if(!isset($this->categoryIcons))
			{
				$this->categoryIcons = cri("MapIcon")->getCategoryIcons();
			}
			$CategoryModel = cri("ListCategory");
			$icon = null;
			$iconUrl = null;
			$categoryId = $listing['Listing']['list_category_id'];
			if(isset($this->categoryIcons[$categoryId]) && $this->categoryIcons[$categoryId])
			{
				$icon = $this->categoryIcons[$categoryId];
			}
			if($icon)
			{
				$iconUrl = cr("cmsConfig.Backend.siteUrl").'/geo_images/'.$icon;
			}
			return $iconUrl;
		}
}
?>